package com.example.demo.security;

import com.example.demo.dao.InsertStudent;
import com.example.demo.dao.UserDao;
import com.example.demo.entity.Student;
import com.example.demo.entity.User;
import com.example.demo.impl.loginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;


@Controller
public class StuController {
    Student stu = new Student();
    User temp = new User();
    private Model model;
    @Autowired
    InsertStudent insertStudent;
    @Autowired
    loginServiceImpl userServiceImpl;
    @RequestMapping("/")
    public String welcome() {
        return "home";
    }


    @GetMapping("/register")
    public String registerForm() {
        return "register";
    }

    @GetMapping("/login")
    public String loginForm() {
        return "login";
    }

    @GetMapping("/personRegister")
    public String toForm(User user){
        userServiceImpl.insertUser(user);
        return "home";
    }
    @RequestMapping("/personLogin")
    public String informationForm(Model model,
                                  @RequestParam("username") String username,
                                  @ModelAttribute("student") Student student,
                                  User user) {
        temp.setUsername(username);
        User user1 =  userServiceImpl.getUser(user.getUsername(),user.getPassword());
        if(user1 == null){
            //JOptionPane.showMessageDialog(null, "用户名或密码错误", "输入错误", JOptionPane.ERROR_MESSAGE);
            return "login";
        }
        model.addAttribute("temp", temp);
        return "Information";
    }
    @GetMapping("/personInformation")
    public String courseForm(Model model,
                             @RequestParam("sname") String sname, @RequestParam("sex") String sex,
                             @RequestParam("sid") Integer sid, @RequestParam("grade") String grade,
                             @RequestParam("college") String college,
                             @RequestParam("major") String major,
                             @ModelAttribute("student") @Valid Student student
                             ) {
        stu.setSname(sname);
        stu.setSex(sex);
        stu.setSid(sid);
        stu.setGrade(grade);
        stu.setCollege(college);
        stu.setMajor(major);
        model.addAttribute("temp", temp);

        return "Course";
    }
    @GetMapping("/personCourse")
    public String resultForm(Model model,
                             @RequestParam("course") String course) {

        stu.setCourse(course);
        model.addAttribute("stu", stu);
        model.addAttribute("temp", temp);
        insertStudent.add(stu);
        return "Result";
    }

    @RequestMapping({"/logout"})
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null) {
            //清除认证
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }
        return "home";
    }
}
